<template>
  <view class="goods-detail-component">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, index) in goods_info.pics" :key="index" @click="openPreviewImage(index)">
        <view class="swiper-item">
          <image :src="item.pics_big" mode="aspectFill"></image>
        </view>
      </swiper-item>
    </swiper>
    <view class="price">
      ￥{{ goods_info.goods_price }}
    </view>
    <view class="body">
      <view class="left">
        {{ goods_info.goods_name }}
      </view>
      <view class="right">
        <uni-icons type="star" size="18" color="lightgray"></uni-icons>
        <text>收藏</text>
      </view>
    </view>
    <view class="fee">
      快递：免运费
    </view>
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <view class="ghost"></view>
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="handleClick" @buttonClick="handleButtonClick" />
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goods_info: {},
        goods_id: 0,
        options: [
          {
            text: '店铺',
            icon: 'shop'
          },
          {
            text: '购物车',
            icon: 'cart',
            info: this.$store.getters.info,
            infoBackgroundColor: '#c00000'
          }
        ],
        buttonGroup: [
          {
            text: '加入购物车',
            backgroundColor: '#c00000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#f5a50a',
            color: '#fff'
          }
        ]
      };
    },
    computed: {
      info() {
        return this.$store.getters.info
      }
    },
    watch: {
      info(newValue, oldValue) {
        this.options.find(value => {
          return value.text === '购物车'
        }).info = newValue
      }
    },
    methods: {
      async getGoodsInfo() {
        const result = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: this.goods_id
        })
        if(result.statusCode !== 200) {
          uni.$showMessage()
        }
        else {
          this.goods_info = result.data.message
        }
      },
      openPreviewImage(index) {
        uni.previewImage({
          urls: this.goods_info.pics.map(value => {
            return value.pics_big
          }),
          current: index
        })
      },
      handleClick(e) {
        if(e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      handleButtonClick(e) {
        if(e.content.text === '加入购物车') {
          const item = {
            goods_id: this.goods_info.goods_id,
            goods_name: this.goods_info.goods_name,
            goods_price: this.goods_info.goods_price,
            goods_count: 1,
            goods_small_logo: this.goods_info.goods_small_logo,
            goods_state: true
          }
          this.$store.dispatch('addToCart', item)
        }
      }
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.getGoodsInfo()
    }
  }
</script>

<style lang="scss" scoped>
.goods-detail-component {
  position: relative;
  swiper {
    height: 750rpx;
    .swiper-item {
      height: 100%;
      image {
        width: 100%;
        height: 100%;
      }
    }
  }
  .price {
    margin: 30rpx 0;
    padding: 0 30rpx;
    color: #c00000;
  }
  .body {
    display: flex;
    justify-content: space-between;
    margin: 30rpx 0;
    padding: 0 30rpx;
    font-size: 26rpx;
    .left {
      width: 600rpx;
    }
    .right {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding-left: 20rpx;
      border-left: 1px solid lightgray;
      color: lightgray;
    }
  }
  .fee {
    margin: 30rpx 0;
    padding: 0 30rpx;
    font-size: 26rpx;
    color: lightgray;
  }
  .ghost {
    height: 50px;
    background-color: transparent;
  }
  .goods-nav {
    position: fixed;
    bottom: 0;
    z-index: 999;
    width: 750rpx;
  }
}
</style>
